using System;
using System.Windows.Forms;
using CastleSample.Business.Client;
using CastleSample.Business.Interface;
using CastleSample.Entity;
using CastleSample.UI.Core;

namespace CastleSample.Modules.Order
{
    public class OrderModule : ModuleBase
    {
        private MenuItemInfo m_OrdersMenu;
        private MenuItemInfo m_CreateOrderMenu;
        private MenuItemInfo m_ListOrderMenu;


        /// <summary>
        /// Version of the module.
        /// Just for display.
        /// </summary>
        public override string Version
        {
            get { return "1.0.0.0"; }
        }

        /// <summary>
        /// Will been called by container when load the module.
        /// </summary>
        public override bool OnLoad()
        {
            m_OrdersMenu = new MenuItemInfo("Order", "Orders");
            m_OrdersMenu.Visible = false;
            m_CreateOrderMenu = new MenuItemInfo("Order_Create", "Create Order", MenuCreateOrder_Clicked);
            m_CreateOrderMenu.ShortcutKeys = Keys.Control | Keys.N;
            m_ListOrderMenu = new MenuItemInfo("Order_ListAll", "List All Order", MenuListOrder_Clicked);
            ApplicationContainer.Instance.MainFrame.RegisterMenuItem(m_OrdersMenu, "File", MenuItemPosition.After);
            ApplicationContainer.Instance.MainFrame.RegisterMenuItem(m_CreateOrderMenu, "Order", MenuItemPosition.ChildFirst);
            ApplicationContainer.Instance.MainFrame.RegisterMenuItem(m_ListOrderMenu, "Order", MenuItemPosition.ChildLast);

            ApplicationContainer.Instance.EventBroker.RegisterEventHandler("UserLoggedIn", UserLoggedIn_EventHandler);
            ApplicationContainer.Instance.EventBroker.RegisterEventHandler("UserLoggedOut", UserLoggedOut_EventHandler);
            ApplicationContainer.Instance.EventBroker.RegisterEventHandler("OrderCreated", OrderCreated_EventHandler);
            ApplicationContainer.Instance.EventBroker.RegisterEventHandler("ViewOrder", ViewOrder_EventHandler);

            return true;
        }

        /// <summary>
        /// Will been called when container loaded all moudules.
        /// </summary>
        public override void OnStartup() {}

        /// <summary>
        /// Will been called by container when unload the module.
        /// </summary>
        public override void OnUnload() {}

        private void UserLoggedIn_EventHandler(object sender, ContainerEventArgs args)
        {
            m_OrdersMenu.Visible = true;
        }

        private void UserLoggedOut_EventHandler(object sender, ContainerEventArgs args)
        {
            m_OrdersMenu.Visible = false;
        }

        private void OrderCreated_EventHandler(object sender, ContainerEventArgs args)
        {
            if (args["OrderEntity"] == null || !(args["OrderEntity"] is OrderEntity))
            {
                return;
            }

            DialogResult ret = MessageBox.Show(ApplicationContainer.Instance.MainFrame, "A order just created, do you want view it?", "CastleSample", MessageBoxButtons.YesNo);
            if (ret == DialogResult.Yes)
            {
                ApplicationContainer.Instance.EventBroker.PublishEvent("ViewOrder", this, args);
                //FormViewOrder form = new FormViewOrder();
                //form.MdiParent = ApplicationContainer.Instance.MainFrame.Form;
                //form.OrderToView = args["OrderEntity"] as OrderEntity;
                //form.Show();
            }
        }

        private void ViewOrder_EventHandler(object sender, ContainerEventArgs args)
        {
            OrderEntity order = null;
            if (args["OrderEntity"] != null && args["OrderEntity"] is OrderEntity)
            {
                order = args["OrderEntity"] as OrderEntity;
            }
            if (order == null && args["OrderId"] != null && args["OrderId"] is Guid)
            {
                order = BizServiceFactory.GetService<IOrderBizService>().FindOrderById((Guid) args["OrderId"]);
            }
            if (order == null)
            {
                return;
            }

            FormViewOrder form = new FormViewOrder();
            form.MdiParent = ApplicationContainer.Instance.MainFrame;
            form.OrderToView = order;
            form.Show();
        }

        private int m_CreateOrderSeq = 0;

        private void MenuCreateOrder_Clicked(object sender, EventArgs args)
        {
            m_CreateOrderSeq++;
            FormCreateOrder form = new FormCreateOrder();
            form.MdiParent = ApplicationContainer.Instance.MainFrame;
            form.Text = string.Format("Create Order {0}", m_CreateOrderSeq);
            form.Show();
        }

        private FormListAllOrders m_FormListAllOrders;

        private void MenuListOrder_Clicked(object sender, EventArgs args)
        {
            if (m_FormListAllOrders != null)
            {
                m_FormListAllOrders.Show();
                return;
            }
            m_FormListAllOrders = new FormListAllOrders();
            m_FormListAllOrders.MdiParent = ApplicationContainer.Instance.MainFrame;
            m_FormListAllOrders.Show();
        }
    }
}